Fix ColumnTransformer.set_output for remainder estimator (swev-id: scikit-learn__scikit-learn-26323)#62
Open
rowan-stein wants to merge 1 commit intoscikit-learn__scikit-learn-26323from
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a bug where
ColumnTransformer.set_outputignores theremainderwhen it is an estimator. As a result, withset_output(transform="pandas"), the remainder path emitted numpy arrays, forcing a fallback tonp.hstackand causing dtype/coercion issues and incorrect final assembly.Fixes: #60
Root Cause
ColumnTransformer.set_outputiterates overself.transformersandself.transformers_, but does not applyset_outputtoself.remainder(when it’s an estimator) in the common pre-fit case. Consequently, the remainder estimator continued to output numpy arrays, mixing types with DataFrame outputs from other transformers and breaking the intended pandas concatenation.Fix
sklearn/compose/_column_transformer.py:ColumnTransformer.set_output, after applying_safe_set_outputto named transformers, also apply it toself.remainderwhen it is an estimator (i.e., not'passthrough'or'drop').set_outputconfiguration (includingtransform='pandas') and enables proper DataFrame concatenation downstream.Reproduction Steps
Using the user-provided example:
Observed Failure (Pre-fix)
Assertion-based test failures before the fix:
After Fix
Tests Added
test_column_transformer_remainder_estimator_set_output_pandastest_column_transformer_remainder_estimator_matches_explicit_transformers_pandasImplementation Details
ColumnTransformer.set_outputinsklearn/compose/_column_transformer.py._safe_set_outputis applied toself.remainderwhen it’s an estimator.remainder='drop': unchanged.remainder='passthrough': identity transformer still handled via existing logic.remainderas aPipeline:Pipeline.set_outputpropagates as expected.Environment Used (Local Validation)
Notes